package com.me.mygdxgame;

import java.util.Vector;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Interpolation.PowOut;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

//GET DIRECTION
enum Directtion {
	D_T, N_B, DB_TN, TB_DN
}

// GET BOUND
// GET BOUND
enum Bound {
	LEFT, RIGHT, TOP, BOTTOM
}

public class Board {
	Vector<Rectangle> m_rects;
	Vector<Cell> m_cells;

	public Board(int x, int y) {
		/*
		 * xet toa do cho tung o tren ban co
		 */
		int x1 = 0;
		int y1 = 500;
		m_rects = new Vector<Rectangle>();
		for (int i = 0; i < 99; i++) {
			m_rects.add(new Rectangle(x + x1, y + y1, 50, 50));
			if ((i + 1) % 9 == 0) {
				x1 = 0;
				y1 = y1 - 50;
			} else {
				x1 = x1 + 50;
			}
		}
		/*
		 * gan cac thuoc tinh khac nhu so cham, id, trang thai cua o tren ban co
		 */
		m_cells = new Vector<Cell>();
		for (int i = 0; i < 99; i++) {
			switch (i) {
			case 0: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 1;
				m_cells.add(m_cell);
				break;
			}
			case 1: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 2;
				m_cells.add(m_cell);
				break;
			}
			case 2: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 3;
				m_cells.add(m_cell);
				break;
			}
			case 3: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 4;
				m_cells.add(m_cell);
				break;
			}
			case 4: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 5;
				m_cells.add(m_cell);
				break;
			}
			case 5: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 6;
				m_cells.add(m_cell);
				break;
			}
			case 6: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 7;
				m_cells.add(m_cell);
				break;
			}
			case 7: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 8;
				m_cells.add(m_cell);
				break;
			}
			case 8: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 9;
				m_cells.add(m_cell);
				break;
			}
			case 13: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 1;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 0;
				m_cells.add(m_cell);
				break;
			}
			case 90: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 9;
				m_cells.add(m_cell);
				break;
			}
			case 91: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 8;
				m_cells.add(m_cell);
				break;
			}
			case 92: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 7;
				m_cells.add(m_cell);
				break;
			}
			case 93: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 6;
				m_cells.add(m_cell);
				break;
			}
			case 94: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 5;
				m_cells.add(m_cell);
				break;
			}
			case 95: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 4;
				m_cells.add(m_cell);
				break;
			}
			case 96: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 3;
				m_cells.add(m_cell);
				break;
			}
			case 97: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 2;
				m_cells.add(m_cell);
				break;
			}
			case 98: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 1;
				m_cells.add(m_cell);
				break;
			}
			case 85: {
				Cell m_cell = new Cell(i);
				m_cell.flag = true;
				m_cell.id = 2;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 0;
				m_cells.add(m_cell);
				break;
			}
			default: {

				Cell m_cell = new Cell(i);
				m_cell.flag = false;
				m_cell.id = 0;
				m_cell.rect = m_rects.get(i);
				m_cell.numberofpoint = 0;
				m_cells.add(m_cell);
				break;
			}

			}
		}

	}

	boolean CheckCellValue(Vector<Cell> vt, int value) {
		boolean flag = false;
		for (int i = 0; i < vt.size(); i++) {
			if (((Cell) vt.get(i)).index == value) {
				flag = true;
			}
		}
		return flag;
	}
	// get bound
	Vector GetBound(Bound b) {
		Vector vt = new Vector();
		switch (b) {
		case RIGHT: {
			for (int i = 8; i < 99; i = i + 9) {
				vt.add(i);
			}
			break;
		}
		case LEFT: {
			for (int i = 0; i <= 90; i = i + 9) {
				vt.add(i);
			}
			break;
		}
		case TOP: {
			for (int i = 0; i < 9; i = i + 1) {
				vt.add(i);
			}
			break;
		}
		case BOTTOM: {
			for (int i = 90; i < 99; i = i + 1) {
				vt.add(i);
			}
			break;
		}
		}
		return vt;
	}

	// lay cac o ma player hien tai co the di qua
	Vector GetCells(int position, Directtion d) {
		Vector result = new Vector();
		Cell currentcell = m_cells.get(position);
		Vector vt_right = GetBound(Bound.RIGHT);
		Vector vt_left = GetBound(Bound.LEFT);
		Vector vt_top = GetBound(Bound.TOP);
		Vector vt_bottom = GetBound(Bound.BOTTOM);
		switch (d) {
		case D_T: {
			int row = position / 9;
			for (int i = row + 1; i < 9 * (row + 1); i++) {
				Cell cell = m_cells.get(i);
				if (cell.flag == false) {
					if (result.size() < cell.numberofpoint)
						result.add(i);
				}
			}
			for (int i = row - 1; i >= 9 * row; i--) {
				Cell cell = m_cells.get(i);
				if (cell.flag == false) {
					if (result.size() < cell.numberofpoint)
						result.add(i);
				}
			}
			break;
		}
		case N_B: {
			for (int i = position + 9; i < 99; i = i + 9) {
				Cell cell = m_cells.get(i);
				System.out.println(cell.flag);
				if (cell.flag == false) {
					if (result.size() < currentcell.numberofpoint)
						result.add(i);
				}
			}

			for (int i = position - 9; i >= 0; i = i - 9) {
				Cell cell = m_cells.get(i);
				if (cell.flag == false) {
					if (result.size() < currentcell.numberofpoint)
						result.add(i);
				}
			}
			break;
		}
		case TB_DN: {
			int origin = position;
			position = position + 10;
			while (!vt_right.contains(position)
					|| !vt_bottom.contains(position)) {
				Cell cell = m_cells.get(position);
				if (cell.flag == false) {
					if (result.size() < cell.numberofpoint)
						result.add(position);
				}
				position = position + 10;
			}
			//
			origin = origin - 10;
			while (!vt_left.contains(origin)|| !vt_top.contains(origin)) {
				Cell cell = m_cells.get(origin);
				if (cell.flag == false) {
					if (result.size() < cell.numberofpoint)
						result.add(origin);
				}
				origin = origin - 10;
			}
			break;
		}
		case DB_TN: {
			int origin = position;
			position = position + 8;
			while (!vt_left.contains(position)
					|| !vt_bottom.contains(position)) {
				Cell cell = m_cells.get(position);
				if (cell.flag == false) {
					if (result.size() < cell.numberofpoint)
						result.add(position);
				}
				position = position + 8;
			}
			//
			origin = origin - 8;
			while (!vt_right.contains(origin) || !vt_top.contains(origin)) {
				Cell cell = m_cells.get(origin);
				if (cell.flag == false) {
					if (result.size() < cell.numberofpoint)
						result.add(origin);
				}
				position = origin - 8;
			}
			break;
		}

		}
		return result;
	}

	/*
	 * get cell near by of current cell
	 */
	Vector GetCellNearBy(int position) {
		Vector result = new Vector();
		Cell currentcell = m_cells.get(position);
		Vector vt_right = GetBound(Bound.RIGHT);
		Vector vt_top = GetBound(Bound.TOP);
		Vector vt_left = GetBound(Bound.LEFT);
		Vector vt_bottom = GetBound(Bound.BOTTOM);
		//right
		if(vt_right.contains(position))
		{
			System.out.println(vt_right.contains(position));
			if(position==((Integer)vt_right.get(0)).intValue())
			{
				result.add(position-1);
				result.add(position+8);
				result.add(position+9);
			}
			else if(position==((Integer)vt_right.get(8)).intValue())
			{
				result.add(position-1);
				result.add(position-10);
				result.add(position-9);
			}
			else
			{
				result.add(position+9);
				result.add(position-9);
				result.add(position-1);
				result.add(position+8);
				result.add(position-10);
			}
		}
		//top
		else if(vt_top.contains(position))
		{
			System.out.println(vt_top.contains(position));
			if(position==((Integer)vt_top.get(0)).intValue())
			{
				result.add(position+1);
				result.add(position+10);
				result.add(position+9);
			}
			else if(position==((Integer)vt_top.get(8)).intValue())
			{
				result.add(position-1);
				result.add(position+8);
				result.add(position+9);
			}
			else
			{
				result.add(position+1);
				result.add(position-1);
				result.add(position+9);
				result.add(position+8);
				result.add(position+10);
			}
		}
		//left
		else if(vt_left.contains(position))
		{
			System.out.println(vt_left.contains(position));
			if(position==((Integer)vt_left.get(0)).intValue())
			{
				result.add(position+1);
				result.add(position+10);
				result.add(position+9);
			}
			else if(position==((Integer)vt_left.get(8)).intValue())
			{
				result.add(position-9);
				result.add(position-8);
				result.add(position+1);
			}
			else
			{
				result.add(position+1);
				result.add(position-9);
				result.add(position-8);
				result.add(position+9);
				result.add(position+10);
			}
		}
		//bottom
		else if(vt_bottom.contains(position))
		{
			System.out.println(vt_bottom.contains(position));
			if(position==((Integer)vt_bottom.get(0)).intValue())
			{
				result.add(position-9);
				result.add(position-8);
				result.add(position+1);
			}
			else if(position==((Integer)vt_bottom.get(8)).intValue())
			{
				result.add(position-1);
				result.add(position-10);
				result.add(position-9);
			}
			else
			{
				result.add(position+1);
				result.add(position-1);
				result.add(position-9);
				result.add(position-8);
				result.add(position-10);
			}
		}
		else
		{
			result.add(position + 1);
			result.add(position - 1);
			result.add(position + 9);
			result.add(position - 9);
			result.add(position + 10);
			result.add(position - 10);
			result.add(position + 8);
			result.add(position - 8);
		}	
		return result;
	}

	int GetCell(float x, float y) {
		int index = 0;
		for (int i = 0; i < m_cells.size(); i++) {
			if (m_cells.get(i).rect.contains(x, y)) {
				index = i;
				break;
			}
		}
		return index;
	}

	// kiem tra 1 diem thuoc cell nao
	public static void main(String[] str) {

		Board b = new Board(100, 30);
		Vector vt = b.GetCellNearBy(20);
		System.out.println(vt);
		// System.out.println(b.GetCell(new Vector2(100,500)));

		// ystem.out.println(b.m_cells.get(i).id + " " +
		// b.m_cells.get(i).rect.x + " " + b.m_cells.get(i).rect.y + " " +
		// b.m_cells.get(i).rect.width + " " +
		// b.m_cells.get(i).rect.height);
	}

}
